﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using PlugNT.Custom;
using PlugNT.Cms.Core;
using PlugNT.Cms.BLL;
using PlugNT.Cms.Model;
using PlugNT.Cms.Page;

namespace PlugNT.Cms.Web.Admin.Pre_User
{
    public partial class EditUser : AdminPage
    {
        private PlugNT.Cms.BLL.User curUser = new PlugNT.Cms.BLL.User();
        private GroupAdmin curGroupAdmin = new GroupAdmin();
        private string action="add";
        private int id=0;

        public string currTitle = "设置用户资料";
        public string jsonString = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            //权限检测
            this.CheckPower();

            id = Request.QueryString["id"] == null ? 0 : int.Parse(Request.QueryString["id"].Trim());

            if (Request.QueryString["t"] != null)
            {
                action=Request.QueryString["t"].Trim().ToLower();
                if(action == "add")
                {
                    currTitle =  "添加用户资料" ;
                }
                else if(action == "edit")
                {
                    currTitle = "修改用户资料";
                }
                else
                {
                    Response.Redirect("../Message.aspx?message=修改用户失败，非法操作！&z=no");
                }
            }
            
            if (!IsPostBack)
            {
                InitData();
            }
        }


        private void InitData()
        {
            DataRow userdr=curUser.GetUserDataRow(id);
            if (userdr != null)
            {
                string html = Sys.Info.usersadminhtml;
                StringBuilder json = new StringBuilder();
                json.Append("{");
                bool firstFlag = true;
                int colCount = userdr.Table.Columns.Count;
                for (int i = 0; i < colCount; i++)
                {
                    string colName = userdr.Table.Columns[i].ColumnName;
                    string colValue = userdr[colName].ToString().Replace("\"", "\\\"");
                    html = html.Replace("val_" + colName, colValue);
                    if (firstFlag)
                    {
                        json.Append("\"nam_" + colName + "\": \"" + colValue + "\"");
                        firstFlag = false;
                    }
                    else
                    {
                        json.Append(",\"nam_" + colName + "\": \"" + colValue + "\"");
                    }
                }
                json.Append("}");
                jsonString = json.ToString();
                litUserHtml.Text = html;  

            }
            else
            {
                Response.Redirect("../Message.aspx?message=修改用户失败！&z=no");
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {

            DataRow userdr = curUser.GetUserDataRow(id);
            if (userdr != null)
            {
                string username = userdr[WebConfig.UsersUserName].ToString();
                string errorMessage = "用户--<b>[" + username + "]</b>--失败！&z=no";
                int formCount = Request.Form.Keys.Count;
                UserInfo uinfo = new UserInfo();

                //防止转换数据失败
                try
                {
                    for (int i = 0; i < formCount; i++)
                    {
                        if (Request.Form.Keys[i].StartsWith("nam_"))
                        {
                            string fieldName = Request.Form.Keys[i].Substring(4);
                            object fieldValue = curUser.ConvertToObject(userdr.Table.Columns[fieldName].DataType, Request.Form["nam_" + fieldName]);
                            uinfo.AddField(fieldName, fieldValue);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Response.Redirect("../Message.aspx?message=修改用户失败->" + ex.Message + "&z=no");
                }


                if (action == "add")
                {
                    if (curUser.Add(uinfo))
                    {
                        Response.Redirect("../Message.aspx?message=添加用户--<b>[" + username + "]</b>--成功！&z=yes");
                    }
                    else
                    {
                        Response.Redirect("../Message.aspx?message=添加" + errorMessage);
                    }
                }
                else
                {
                    if (curUser.Update(id,uinfo))
                    {
                        Response.Redirect("../Message.aspx?message=修改用户--<b>[" + username + "]</b>--成功！&z=yes");
                    }
                    else
                    {
                        Response.Redirect("../Message.aspx?message=修改" + errorMessage);
                    }
                }
            }
            else
            {
                Response.Redirect("../Message.aspx?message=修改用户失败，该用户已被删除或不存在！&z=no");
            }

        }

    }
}
